package cn.percent.service.impl;

import cn.percent.annotation.Autowired;
import cn.percent.annotation.Service;
import cn.percent.annotation.Transactional;
import cn.percent.dao.AccountDao;
import cn.percent.pojo.Account;
import cn.percent.service.TransferService;

import java.sql.SQLException;

@Transactional
@Service(value = "transferService")
public class TransferServiceImpl implements TransferService {
    @Autowired
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao){
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws SQLException {
        Account from = null;
        System.out.println(money);
        try {
            from = accountDao.queryAccountByCardNo(fromCardNo);

        }catch (Exception e){
            e.printStackTrace();
        }
        Account to = accountDao.queryAccountByCardNo(toCardNo);
        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);
        System.out.println(from.getMoney());
        System.out.println(to.getMoney());

        accountDao.updateAccountByCardNo(to);
        accountDao.updateAccountByCardNo(from);
    }
}
